﻿using Bll;
using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Myproject.Menu
{
    /// <summary>
    /// MenuAjax 的摘要说明
    /// </summary>
    public class MenuAjax : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //if (!string.IsNullOrEmpty(cookieHelp.AdminId))
            if (true)
            {
                //if (new AdminBLL().ExistsByAdminName(cookieHelp.AdminName) && new AdminBLL().ExistsByAdminId(cookieHelp.AdminId))
                if (true)
                {
                    string dos = context.Request.Params["dos"];
                    string id;
                    #region swith do
                    switch (dos)
                    {
                        case "delete":
                            id = context.Request.QueryString["id"];
                            if (new MenuBll().Delete(Int32.Parse(id)))
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "删除成功！", "menu_menuList", "", "", false, "", ""));
                            }
                            else
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "删除失败！", "menu_menuList", "", "", false, "", ""));
                            }
                            break;

                        case "rank":
                            id = context.Request.QueryString["id"];
                            string rank = context.Request.QueryString["rank"];
                            if (id != "0")
                            {
                                MenuModel model = new MenuBll().GetModel(int.Parse(id));
                                model.HtmlRank += int.Parse(rank);
                                if (new MenuBll().Update(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改排序成功！", "menu_menuList", "", "", false, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改排序失败！", "menu_menuList", "", "", false, "", ""));
                                }
                            }
                            else
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改排序失败！", "menu_menuList", "", "", false, "", ""));
                            }
                            break;

                        case "show":
                            id = context.Request.QueryString["id"];
                            string show = context.Request.QueryString["show"];
                            if (id != "0")
                            {
                                MenuModel model = new MenuBll().GetModel(int.Parse(id));
                                model.MenuFlag = bool.Parse(show);
                                if (new MenuBll().Update(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改成功！", "menu_menuList", "", "", false, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改失败！", "menu_menuList", "", "", false, "", ""));
                                }
                            }
                            else
                            {
                                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改失败！", "menu_menuList", "", "", false, "", ""));
                            }
                            break;

                        case "edit":
                            string MenuID = context.Request.Params["menu_menuEdit_id"].Trim();
                            string MenuPID = context.Request.Params["menu_menuEdit_pid"].Trim();
                            string MenuTID = context.Request.Params["menu_menuEdit_tid"].Trim();
                            string MenuName = context.Request.Params["menu_menuEdit_name"].Trim();
                            string MenuRank = context.Request.Params["menu_menuEdit_rank"].Trim();
                            string MenuUrl = context.Request.Params["menu_menuEdit_url"].Trim();
                            string MenuIco = context.Request.Params["menu_menuEdit_ico"].Trim();
                            string MenuTabID = context.Request.Params["menu_menuEdit_tabid"].Trim();
                            if (MenuID == "0")
                            {
                                MenuModel model = new MenuModel();
                                model.MenuName = MenuName;
                                model.MenuTypeID = Int32.Parse(MenuTID);
                                model.Pid = Int32.Parse(MenuPID);
                                model.HtmlRank = Int32.Parse(MenuRank);
                                model.HtmlUrl = MenuUrl;
                                model.HtmlFaicon = MenuIco;
                                model.HtmlTabid = MenuTabID;

                                if (new MenuBll().Add(model)>0)
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "新增成功！", "menu_menuList", "", "", true, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "新增失败！", "menu_menuList", "", "", true, "", ""));
                                }
                            }
                            else
                            {
                                MenuModel model = new MenuBll().GetModel(Int32.Parse(MenuID));
                                model.MenuName = MenuName;
                                model.MenuTypeID = Int32.Parse(MenuTID);
                                model.Pid = Int32.Parse(MenuPID);
                                model.HtmlRank = Int32.Parse(MenuRank);
                                model.HtmlUrl = MenuUrl;
                                model.HtmlFaicon = MenuIco;
                                model.HtmlTabid = MenuTabID;
                                if (new MenuBll().Update(model))
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(200, "修改成功！", "menu_menuList", "", "", true, "", ""));
                                }
                                else
                                {
                                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(300, "修改失败！", "menu_menuList", "", "", true, "", ""));
                                }
                            }
                            break;
                    }
                    #endregion
                }
                else
                {
                    context.Response.Write(ReturnJsonHelp.BJUIajaxJson(301, "登陆超时！", "", "", "", false, "/bsm/logFrom.aspx", "登陆超时！"));
                }
            }
            else
            {
                context.Response.Write(ReturnJsonHelp.BJUIajaxJson(301, "登陆超时！", "", "", "", false, "/bsm/logFrom.aspx", "登陆超时！"));
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}